<template>
  <view>
    <view class="category_tab">
      <view class="category_tab_title">
        <view class="title_inner">
          <uni-segmented-control
            :current="current"
            :values="items.map(v=>v.title)"
            @clickItem="onClickItem"
            style-type="text"
            active-color="#d4237a"
          ></uni-segmented-control>
        </view>
        <view class="iconfont iconsearch"></view>
      </view>
      <scroll-view scroll-y enable-flex @scrolltolower="handleToLower" class="category_tab_content">
        <view class="cate_item" v-for="item in vertical" :key="item.id">
          <image :src="item.thumb" mode="widthFix" />
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import { uniSegmentedControl } from "@dcloudio/uni-ui"; //分段器组件
export default {
  onLoad(options) {
    this.id = options.id;
    this.getList();
  },
  components: {
    uniSegmentedControl //在组件标签中声明分段器
  },
  data() {
    return {
      items: [
        { title: "最新", order: "new" },
        { title: "热门", order: "hot" }
      ],
      current: 0,
      params: {
        limit: 30,
        skip: 0,
        order: "new"
      },
      id: 0, //上一页面传递过来的参数
      vertical: [], //接口返回数据(页面渲染数据)
      //是否还有下一页
      hasMore: true
    };
  },
  methods: {
    //标题切换
    onClickItem(e) {
      //根据被点击的标题 切换标题的选中
      if (this.current !== e.currentIndex) {
        this.current = e.currentIndex;

        //根据被点击的标题 切换请求参数中的order属性值
        this.params.order = this.items[e.currentIndex].order;
        //初始化skip属性的值
        this.params.skip = 0;
        //初始化页面要渲染的数据数组
        this.vertical = [];
        //初始化参数
        this.hasMore = true;
        //重新发送请求
        this.getList();
      }
    },
    getList() {
      this.request({
        url: `http://157.122.54.189:9088/image/v1/vertical/category/${this.id}/vertical`,
        data: this.params
      }).then(result => {
        if (result.res.vertical.length === 0) {
          this.hasMore = false;
          uni.showToast({
            title: "没有数据了",
            icon: "none"
          });
          return;
        }

        this.vertical = [...this.vertical, ...result.res.vertical];
      });
    },
    //滚动条触底事件(做分页效果)
    handleToLower() {
      if (this.hasMore) {
        //修改请求参数
        this.params.skip += this.params.limit;
        //发送请求 获取数据
        this.getList();
      } else {
        uni.showToast({
          title: "没有数据了",
          icon: "none"
        });
      }
    }
  }
};
</script>


<style lang="scss">
.category_tab {
  .category_tab_title {
    position: relative;
    .title_inner {
      width: 60%;
      margin: 0 auto;
    }

    .iconfont {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      right: 5%;
    }
  }

  .category_tab_content {
    display: flex;
    flex-wrap: wrap; //换行属性
    height: calc(100vh - 36px);
    .cate_item {
      width: 33.3%;
      border: 5rpx solid #fff;
      image {
      }
    }
  }
}
</style>